package com.shenhaofeng.app.loader.content.component;

import android.os.Bundle;
import android.os.Looper;
import android.view.View;

import com.shenhaofeng.app.loader.content.Component;
import com.shenhaofeng.app.loader.content.ContentSource;
import com.shenhaofeng.app.loader.content.OnBindContentListener;
import com.shenhaofeng.app.widget.status.ViewStatusSwitcher;

import java.util.concurrent.Executor;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by haofeng on 2016/9/28.
 */

public class StatusSwitechComponent<R> extends Component<R> {


    private OnErrorStatusListener mOnErrorStatusListener;

    public StatusSwitechComponent(OnErrorStatusListener mOnErrorStatusListener) {
        this.mOnErrorStatusListener = mOnErrorStatusListener;
    }

    public StatusSwitechComponent(Looper mLooper, OnErrorStatusListener mOnErrorStatusListener) {
        super(mLooper);
        this.mOnErrorStatusListener = mOnErrorStatusListener;
    }


    public StatusSwitechComponent(Executor mExecutor, OnErrorStatusListener mOnErrorStatusListener) {
        super(mExecutor);
        this.mOnErrorStatusListener = mOnErrorStatusListener;
    }

    @Override
    protected void onPreLoad(View statusView, View contentView) {
        final ViewStatusSwitcher switcher = (ViewStatusSwitcher) statusView;
        switcher.post(new Runnable() {
            @Override
            public void run() {
                switcher.showLoading();
            }
        });
    }

    @Override
    protected void onLoad(View statusView, final View contentView, Bundle args, final OnBindContentListener<R> listener, ContentSource<R> contentSource) {
        final ViewStatusSwitcher switcher = (ViewStatusSwitcher) statusView;
        contentSource.onRequestContent(args)
                .subscribeOn(getWorkScheduler())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<R>() {
                    @Override
                    public void call(final R r) {
                        switcher.post(new Runnable() {
                            @Override
                            public void run() {
                                switcher.showContent();
                                listener.onBindContent(contentView, r);
                            }
                        });

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(final Throwable throwable) {
                        switcher.post(new Runnable() {
                            @Override
                            public void run() {
                                switcher.showError(mOnErrorStatusListener.onHandleError(throwable));
                            }
                        });
                    }
                });

    }


    @Override
    protected void onInterruptLoad(View statusView, View contentView) {
        final ViewStatusSwitcher switcher = (ViewStatusSwitcher) statusView;
        switcher.post(new Runnable() {
            @Override
            public void run() {
                switcher.showLoading();
            }
        });
    }

    @Override
    protected boolean hasContentView(View statusView, View contentView) {
        return statusView instanceof ViewStatusSwitcher;
    }


    public interface OnErrorStatusListener {
        int onHandleError(Throwable e);
    }

}
